package persistence;

import games.CupMatch;
import games.SimpleMatch;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import players.FutbolPlayer;
import players.Position;
import players.Skill;
import players.SkillImpl;
import teams.Formation;

/**
 * Generador de datos.
 */
public class DataGenerator {
    private static DataGenerator inst = new DataGenerator();

    private List<Position> allPositions;

    private DataGenerator() {
        this.allPositions = new ArrayList<Position>();
        this.allPositions.add(Position.GOALKEEPER);
        this.allPositions.add(Position.CENTREBACK);
        this.allPositions.add(Position.LEFTBACK);
        this.allPositions.add(Position.RIGHTBACK);
        this.allPositions.add(Position.DEFMIDFIELDER);
        this.allPositions.add(Position.SECONDARYSTRIKER);
        this.allPositions.add(Position.ATTACKMIDFIELDER);
        this.allPositions.add(Position.FORWARD);
    }

    public static DataGenerator getInstance() {
        return inst;
    }

    public int generateRandomValue(final int values) {
        Random rnd = new Random();
        return rnd.nextInt(values);
    }

    public List<FutbolPlayer> createFutbolPlayers(final List<String> names, final List<Skill> skills) {
        List<FutbolPlayer> allPlayers = new ArrayList<FutbolPlayer>();
        for (String name : names) {
            FutbolPlayer currentPlayer = new FutbolPlayer(name);
            for (int i = 0; i < 3; i++) {
                currentPlayer.addSkill(skills.get(this.generateRandomValue(skills.size())));
            }
            allPlayers.add(currentPlayer);
        }
        return allPlayers;
    }

    public List<Skill> createSkillList() {
        List<Skill> randomSkills = new ArrayList<Skill>();
        for (Position currentPosition : this.allPositions) {
            for (int i = 0; i < 5; i++) {
                randomSkills.add(new SkillImpl(currentPosition, this.generateRandomValue(11)));
            }
        }
        return randomSkills;
    }

    public SimpleMatch generateSimpleMatch(final Formation aFormation, final Formation anotherFormation) {
        int goals1 = this.generateRandomValue(7);
        int goals2 = this.generateRandomValue(7);
        while (goals1 + goals2 > 6) {
            goals1 = this.generateRandomValue(7);
            goals2 = this.generateRandomValue(7);
        }
        return new SimpleMatch(aFormation, anotherFormation, goals1, goals2);

    }

    public CupMatch generateCupMatch(final Formation aFormation, final Formation anotherFormation) {
        CupMatch match = new CupMatch(this.generateSimpleMatch(aFormation, anotherFormation), this.generateSimpleMatch(
                aFormation, anotherFormation));
        if (match.havePenalty()) {
            this.doPenalties(match);
        }
        return match;
    }

    private CupMatch doPenalties(final CupMatch aCupMatch) {
        int penaltyGoals1 = 0;
        int penaltyGoals2 = 0;
        int rounds = 0;
        boolean haveWinner = false;
        while (!haveWinner) {
            penaltyGoals1 += this.generateRandomValue(2);
            penaltyGoals2 += this.generateRandomValue(2);
            rounds++;
            if (rounds >= 5) {
                if (penaltyGoals1 != penaltyGoals2) {
                    haveWinner = true;
                } else {
                    rounds--;
                }
            }
        }
        aCupMatch.setPenaltyGoals1(penaltyGoals1);
        aCupMatch.setPenaltyGoals2(penaltyGoals2);
        return aCupMatch;
    }
}
